//
//  Open_ClassViewController.m
//  OpenYooxi_IOS
//
//  Created by YeahYes on 15/3/16.
//  Copyright (c) 2015年 com.ivyvi.ee. All rights reserved.
//

#import "Open_ClassViewController.h"
#import "RightView_CollectionViewCell.h"
#import "FenLeiXiangQing_ViewController.h"

static NSString *identify = @"cell";
static NSString *identifyconlloction = @"conlloctioncell";

@interface Open_ClassViewController (){
    NSArray *shuarray;
    
    BOOL isselect;
    
    NSIndexPath  *indexPathjishu;
    
    UICollectionViewFlowLayout *flowLayout;

}
@end

@implementation Open_ClassViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    shuarray = @[@"手机数码",@"潮流女装",@"品牌男装",@"内衣配饰",@"家用电器",@"电脑办公",@"家装建材",@"生鲜食品",@"母婴产品",@"进口奶粉",];
    //1.自定义标题视图
    UIView *navview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-36, 34)];
    navview.layer.cornerRadius = 4;
    navview.layer.masksToBounds = YES;
    navview.layer.borderColor = UIColorFromRGB(0xE5E5E5).CGColor;
    navview.layer.borderWidth = 1;
    
    UIImageView *suoimg = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 18, 18)];
    suoimg.image = [UIImage imageNamed:@"9_03"];
    [navview addSubview:suoimg];
    
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(34, 2, kScreenWidth-76, 30)];
    textField.borderStyle = UITextBorderStyleNone;
    textField.font = [UIFont systemFontOfSize:13];
    textField.textColor = [UIColor blackColor];
    textField.placeholder = @"搜索您感兴趣的商品";
    [navview addSubview:textField];
    
    navview.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView = navview;
    
    
//    _leftView.backgroundColor = UIColorFromRGB(0xF1F5F5);
    _leftView.backgroundColor = [UIColor whiteColor];

    
//    _leftView.layer.borderColor = UIColorFromRGB(0xE5E5E5).CGColor;
//    _leftView.layer.borderWidth = 1;
    
    _rightView.backgroundColor = [UIColor clearColor];
    
    /**UITableView,UIScrollView的时候有时候会偏移20像素
     　　在ios7里面如果有滚动视图的时候，比如UITableView,UIScrollView的时候有时候会偏移20像素。这时候可以这样设置*/
    if ([[UIDevice currentDevice] systemVersion].floatValue>=7.0) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    _lefttableview.dataSource = self;
    _lefttableview.delegate = self;
    //.隐藏UITableViewCell的分隔线
    _lefttableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _lefttableview.backgroundColor = [UIColor clearColor];
    
    //取消tableview右侧的滑动条
    _lefttableview.showsVerticalScrollIndicator = NO;
    
//    [_lefttableview registerClass:[UITableView class] forCellReuseIdentifier:identify];
    
    
    
    
    //1.为当前UICollectionView对象，创建布局对象
    flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //2.设置滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(_rightView.frame.size.width/2, 140);
    
    //3.minimumInteritemSpacing  水平方向单元格之间的最小间隙
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;

    
    //取消UICollectionView右侧的滑动条 上下滑动是设置：showsVerticalScrollIndicator
    _rightcollectionview.showsVerticalScrollIndicator = NO;
    _rightcollectionview.collectionViewLayout = flowLayout;
    _rightcollectionview.dataSource = self;
    _rightcollectionview.delegate = self;
//    _rightcollectionview.pagingEnabled = YES;  //分页
    _rightcollectionview.backgroundColor = [UIColor whiteColor];
    
    //注册单元格类型
    UINib *nib = [UINib nibWithNibName:@"RightView_CollectionViewCell" bundle:[NSBundle mainBundle]];
    [_rightcollectionview registerNib:nib forCellWithReuseIdentifier:identifyconlloction];
}


#pragma mark - UICollectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 13;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    RightView_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifyconlloction forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor clearColor];
    return cell;
}

#pragma mark -collectionView的代理方法点击触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FenLeiXiangQing_ViewController *fenleixiangqing = [[FenLeiXiangQing_ViewController alloc] initWithNibName:@"FenLeiXiangQing_ViewController" bundle:nil];
    [self.navigationController pushViewController:fenleixiangqing animated:YES];
}




#pragma mark - UITableView delegate 设置单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return shuarray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    flowLayout.itemSize = CGSizeMake(_rightView.frame.size.width/2, 140);


    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld",(long)[indexPath section], (long)[indexPath row]];//以indexPath来唯一确定cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.layer.borderColor = UIColorFromRGB(0xE5E5E5).CGColor;
    cell.layer.borderWidth = 1;
    cell.contentView.backgroundColor = UIColorFromRGB(0xF1F5F5);
    
    //改变UITableViewCell选中时背景色
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    
    cell.textLabel.text = shuarray[indexPath.row];
    cell.textLabel.font =[UIFont systemFontOfSize:13];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    if(isselect == NO){
        if(indexPath.row ==0){
            cell.backgroundColor = [UIColor whiteColor];
            cell.layer.borderWidth = 0;
            //默认状态下的选中
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            
//            _lefttableview
            if ([_lefttableview.delegate respondsToSelector:@selector(tableView:willSelectRowAtIndexPath:)]) {
                
                [_lefttableview.delegate tableView:tableView willSelectRowAtIndexPath:indexPath];
            }
            [_lefttableview selectRowAtIndexPath:indexPath animated:YES scrollPosition: UITableViewScrollPositionNone];
            
            
            if ([_lefttableview.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
                
                [_lefttableview.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
                
            }

            
        }
    }

    return cell;
}


#pragma mark - UITableViewDelegate  单元格的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.row !=0){
        isselect = YES;
        NSIndexPath  *indexPathx = [NSIndexPath indexPathForRow:0 inSection:0];
        UITableViewCell *cellx =  [tableView cellForRowAtIndexPath:indexPathx];
        cellx.backgroundColor = [UIColor whiteColor];
        cellx.layer.borderWidth = 1;
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.layer.borderWidth = 0;
        if(indexPathjishu != indexPath){
            UITableViewCell *celljishu = [tableView cellForRowAtIndexPath:indexPathjishu];
            celljishu.layer.borderWidth = 1;
        }
        
        //数据的加载和值的传递
        [_rightcollectionview reloadData];
        
    }else if (indexPath.row == 0){
        NSIndexPath  *indexPathx = [NSIndexPath indexPathForRow:0 inSection:0];
        UITableViewCell *cellx =  [tableView cellForRowAtIndexPath:indexPathx];
        cellx.backgroundColor = [UIColor whiteColor];
        cellx.layer.borderWidth = 0;
        
        UITableViewCell *celljishu = [tableView cellForRowAtIndexPath:indexPathjishu];
        celljishu.layer.borderWidth = 1;
        
        
        //数据的加载和值的传递
        [_rightcollectionview reloadData];

    }
    
    
    indexPathjishu = indexPath;

}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
